1 module hip.data.jsonc; 2 3 4 public import hip.data.json; 5 6 auto parseJSONC()(string jsonc) 7 { 8 version(WebAssembly) static assert(false, "No JSONC for Wasm."); 9 return parseJSON(stripComments(jsonc)); 10 } 11 12 /** 13 * Strips single and multi line comments (C style) 14 */ 15 string stripComments(string str) 16 { 17 string ret; 18 size_t i = 0; 19 size_t length = str.length; 20 ret.reserve(str.length); 21 22 while(i < length) 23 { 24 //Don't parse comments inside strings 25 if(str[i] == '"') 26 { 27 size_t left = i; 28 i++; 29 while(i < length && str[i] != '"') 30 { 31 if(str[i] == '\\') 32 i++; 33 i++; 34 } 35 i++; //Skip '"' 36 ret~= str[left..i]; 37 } 38 //Parse single liner comments 39 else if(str[i] == '/' && i+1 < length && str[i+1] == '/') 40 { 41 i+=2; 42 while(i < length && str[i] != '\n') 43 i++; 44 } 45 //Parse multi line comments 46 else if(str[i] == '/' && i+1 < length && str[i+1] == '*') 47 { 48 i+= 2; 49 while(i < length) 50 { 51 if(str[i] == '*' && i+1 < length && str[i+1] == '/') 52 break; 53 i++; 54 } 55 i+= 2; 56 } 57 //Safe check to see if it is in range 58 if(i < length) 59 ret~= str[i]; 60 i++; 61 } 62 return ret; 63 }